package com.cn.wanxi.util;

import java.io.*;
import java.util.List;

public class IOReadWrite {
    public static void writeNote(String fileName, List<String> data) {
        File directory = new File(Path.PATH.getPath());
        if (!directory.exists()) {
//            file.mkdir();//
            directory.mkdirs();
        }
        File file = new File(directory, fileName);
        try {
            file.createNewFile();//创建文件
            OutputStream outputStream = new FileOutputStream(file);

            for (int i = 0; i < data.size(); i++) {
                outputStream.write(data.get(i).getBytes());
                outputStream.write(Path.SPLIT.getPath().getBytes());
            }
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取IO数据
     *
     * @param fineName
     * @return
     */
    public String readNote(String fineName) {
        File file = new File(Path.PATH.getPath(), fineName);
        StringBuilder stringBuilder = new StringBuilder();
        try {
            Reader reader = new FileReader(file);
            int result = 0;
            while ((result = reader.read()) != -1) {
                stringBuilder.append((char) result);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
